<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

use app\common\model\UserAccount;
use think\Db;

/**
 * Notice逻辑
 */
class Notice extends LogicBase
{
    

    
    /**
     * 获取Notice列表
     */
    public function getNoticeList($where = [], $field = 'a.*', $order = '')
    {
        
        $this->modelNotice->alias('a');
        

        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $count=$this->modelNotice->where($where)->count();
        $this->modelNotice->alias('a');
        return ['list'=>$this->modelNotice->getList($where, $field, $order),'count'=>$count] ;
    }
    
    /**
     * 获取Notice列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.title'] = ['like', '%'.$data['search_data'].'%'];
        !empty($data['cate_id']) && $where['a.cate_id'] = $data['cate_id'];
        return $where;
    }
    
    /**
     * Notice信息编辑
     */
    public function NoticeEdit($data = [])
    {
        
        $validate_result = $this->validateNotice->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateNotice->getError()];
        }
        
        $url = url('NoticeList');

        preg_match_all('/<img.*?src="(.*?)".*?\/?>/i', $data['content'], $out, PREG_PATTERN_ORDER);      //正则匹配img标签的src属性，返回二维数组
        if (!empty($out[1])) {
            foreach ($out[1] as $v) {
                    if(!preg_match('/http/',$v)){
                        $url1 = DMOAIN_URL.$v;
                        $data['content'] = str_replace($v, $url1, $data['content']);   //替换相对路径为绝对路径
                    }

            }
        }
        if(!isset($data['is_push'])){
            $data['is_push']=0;
        }

        $result = $this->modelNotice->setInfo($data);

        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, 'Notice' . $handle_text . '，title：' . $data['title']);
        
        return $result ? [RESULT_SUCCESS, 'Notice操作成功', $url] : [RESULT_ERROR, $this->modelNotice->getError()];
    }

    /**
     * 获取Notice信息
     */
    public function getNoticeInfo($where = [], $field = 'a.*')
    {
        
        $this->modelNotice->alias('a');
        

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelNotice->getInfo($where, $field);
    }
    


    /**

    
    /**
     * Notice删除
     */
    public function NoticeDel($where = [])
    {
        
        $result = $this->modelNotice->deleteInfo($where);
        
        $result && action_log('删除', 'Notice删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, 'Notice删除成功'] : [RESULT_ERROR, $this->modelNotice->getError()];
    }


    //导入excel
    public function excel(){

//       set_time_limit(0);
        $excel = request()->file('excel')->getInfo();//excel为file中的name
        vendor("PHPExcel.PHPExcel.IOFactory");
        $objPHPExcel = \PHPExcel_IOFactory::load($excel['tmp_name']);//读取上传的文件
        $arrExcel = $objPHPExcel->getSheet(0)->toArray();//获取其中的数据

        $word = $arrExcel[0];
        array_shift($arrExcel);

        $num = count($arrExcel) -1;
//        d($arrExcel);
        Db::startTrans();

        foreach ($arrExcel as $key => $value) {
            $data=[
                'username'=>$value[1],
                'nickname'=>$value[1],
                'password'=>$value[1],
            ];

            if($this->modelMember->getInfo(['username'=>$value[1]])){
                continue;
            }
            try{
                if($value[3] && $member=$this->modelMember->getInfo(['username'=>$value[3]])){
                  $data['leader_id']=$member['id'];
                }
                //注册
                $id=\app\common\model\Member::create($data);
                //创建钱包
                UserAccount::addWallet($id->id);
                Db::commit();
            }catch (\Exception $e){
                echo $e->getMessage();
                Db::rollback();

            }

        }



           return  $this->success('导入成功');

    }



    /**
     * 获取分类信息
     */
    public function getnoticeCategoryInfo($where = [], $field = true)
    {

        return $this->modelnoticeCategory->getInfo($where, $field);
    }

    /**
     * 获取公告分类列表
     */
    public function getnoticeCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {
        $count=$this->modelnoticeCategory->where($where)->count();
        $list=$this->modelnoticeCategory->getList($where, $field, $order, $paginate);
        return ['count'=>$count,'list'=>$list];
    }

    /**
     * 公告分类编辑
     */
    public function noticeCategoryEdit($data = [])
    {



            $url = url('noticeCategoryList');



        $result = $this->modelnoticeCategory->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '公告分类' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelnoticeCategory->getError()];
    }

    /**
     * 公告分类删除
     */
    public function noticeCategoryDel($where = [])
    {

        $result = $this->modelnoticeCategory->deleteInfo($where);

        $result && action_log('删除', '公告分类删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '公告分类删除成功'] : [RESULT_ERROR, $this->modelnoticeCategory->getError()];
    }

}
